﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;

public class carddatabase : MonoBehaviour
{
    public List<Card> database = new List<Card>();
    public JsonData carddata;

    void Start()
    {
        carddata = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Card.json"));
        carddatacon();
    }

    public Card fetchcardbyname(string name)
    {
        for (int i = 0; i < database.Count; i++)
            if (database[i].Name.Contains(name))
            {
                return database[i];
            }
        return null;
    }
    void carddatacon()
    {
        for (int i = 0; i < carddata.Count; i++)
        {
            database.Add(new Card((int)carddata[i]["id"], carddata[i]["name"].ToString(), (int)carddata[i]["pow"], (int)carddata[i]["grade"],
            (int)carddata[i]["sld"], carddata[i]["des"].ToString(), carddata[i]["type"].ToString(), carddata[i]["nation"].ToString(),
            carddata[i]["clan"].ToString(), carddata[i]["race"].ToString(), carddata[i]["damage"].ToString(), (int)carddata[i]["cardmax"],
            carddata[i]["skill"].ToString(), carddata[i]["slug"].ToString(), (bool)carddata[i]["stackable"]));
        }
    }
}
public class Card
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Pow { get; set; }
    public int Grade { get; set; }
    public int Sld { get; set; }
    public string Des { get; set; }
    public string Type { get; set; }
    public string Nation { get; set; }
    public string Clan { get; set; }
    public string Race { get; set; }
    public string Damage { get; set; }
    public int Cardmax { get; set; }
    public string Skill { get; set; }
    public string Slug { get; set; }
    public Sprite Sprite { get; set; }
    public bool Stackable { get; set; }

    public Card(int id, string name, int pow, int grade, int sld, string des, string type, string nation,
            string clan, string race, string damage, int cardmax, string skill, string slug, bool stackable)
    {
        this.Id = id;
        this.Name = name;
        this.Pow = pow;
        this.Sld = sld;
        this.Grade = grade;
        this.Des = des;
        this.Type = type;
        this.Nation = nation;
        this.Clan = clan;
        this.Race = race;
        this.Damage = damage;
        this.Cardmax = cardmax;
        this.Skill = skill;
        this.Slug = slug;
        this.Sprite = Resources.Load<Sprite>("Cardimg/" + slug);
        this.Stackable = stackable;
    }
    public Card()
    {
        this.Id = -1;
        this.Name = null;
    }
}